<?php
namespace app\user\controller;
use think\Controller;
use app\common\controller\Base;
use app\admin\model\Users;
class Register extends Base{
    public function _initialize(){
        parent::_initialize();
        $this->assign('lang', $this->lang);
    }
    public function index(){
        if(request()->isPost()) {
            $data = input('post.');
            $data['email'] = trim($data['email']);
            if(empty($data['email']) || !is_email($data['email'])){
                return array('code'=>0,'msg'=>lang('Please enter the mailbox address'));
            }
            /*判断邮箱是否唯一*/
            if(Users::validEmail($data['email'])){
            	return array('code'=>-1,'msg'=>lang('this email has been used'));
            }
            //验证两次密码是否匹配
            if($data['password'] != $data['password2']){
                return array('code'=>-1,'msg'=>lang('The two input password is not consistent'));
            }
           
            if(empty($data['mobile']) || !is_mobile($data['mobile'])){
                return array('code'=>0,'msg'=>lang('Please enter the correct phone number'));
            }
            if(!empty($data['telephone']) && !preg_match("/^[0-9\-]{6,14}$/",$data['telephone'])){
                return array('code'=>0,'msg'=>lang('Please enter the correct landline number'));
            }
            if(empty($data['company'])){
                return array('code'=>0,'msg'=>lang('Please enter your company name'));
            }
            $data['password'] = $data['pwd'] = md5($data['password']);
            $data['add_time'] = time();
            $data['is_open'] = 1;
            $data['group_id'] = config('customer_id'); //客户
            $data['lang'] = $this->lang;
            $data['source'] = 'CF';//CMP
            if(db('users')->data($data)->insert() === false){
                
                return array('code'=>-1,'msg'=>lang('registration failed'));
            }
            /*同步到ucenter*/
            $ucenter = new \clt\Ucenter(); 
            $uid = $ucenter->register($data);
            return array('code'=>1,'msg'=>lang('Success'),'result'=>$user);
        }else {
            $this->assign('title', lang('Sign up'));
            return $this->fetch();
        }
    }
}